package edu.gatech.cs2340.group7.wheresmystuffapp.presenters;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.Account;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.CustomDate;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.DataManager;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.DatabaseHelper;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.FilterResult;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.Item;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.ItemManager;
import edu.gatech.cs2340.group7.wheresmystuffapp.views.ReportedItemsView;

/**
 * Presenter for showing an accounts reported items
 * @author tbowling3
 *
 */
public class ReportedItemsPresenter {

	private ReportedItemsView view;
	private TableLayout layout;
	private Account account;
	private ArrayList<Item> ListLostItems;
	private ArrayList<Item> ListFoundItems;
	private List<Item> ListOfReportedItems;
	private ItemManager manager;

	public ReportedItemsPresenter(ReportedItemsView v) {
		view = v;
		layout = new TableLayout(v);
		manager = new ItemManager();
		account = DataManager.getCurrentAccount();
	}

	/**
	 * Sets up the page - this is being saved for a later date
	 * @return the layout for the page
	 */
	public View setupPage() {
		FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		layout.setLayoutParams(lp);
		layout.setStretchAllColumns(false);

		TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
		TableRow.LayoutParams cellLp = new TableRow.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
		setUpTitle(cellLp, rowLp);

		setListOfItems();

		addReportedItems(cellLp, rowLp);
		// for (int r = 0; r < 2; ++r)
		// {
		// TableRow row = new TableRow(view);
		// for (int c = 0; c < 2; ++c)
		// {
		// Button btn = new Button(view);
		// btn.setText("A");
		// row.addView(btn, cellLp);
		// }
		// layout.addView(row, rowLp);
		// }

		return layout;
	}

	/**
	 * This sets up the title of the reported Items Screen
	 * 
	 * @param cellLp
	 * @param rowLp
	 */
	private void setUpTitle(TableRow.LayoutParams cellLp,
			TableLayout.LayoutParams rowLp) {
		TextView title = new TextView(view);
		title.setText("Item's You've Reported");
		TableRow row1 = new TableRow(view);
		row1.addView(title, cellLp);

		TableRow row2 = new TableRow(view);
		TextView LostItems = new TextView(view);
		TextView FoundItems = new TextView(view);
		LostItems.setText("Lost Items");
		FoundItems.setText("Found Items");
		// space = new Space(view);

		row2.addView(LostItems, cellLp);
		// row2.addView(space, cellLp);
		row2.addView(FoundItems, cellLp);

		layout.addView(row1, rowLp);
		layout.addView(row2, rowLp);
	}

	/**
	 * This gets a list of items reported lost and a list of items reported
	 * found by the user
	 */
	private void setListOfItems() {
		//Account account = DataManager.getCurrentAccount();
		ListLostItems = new ArrayList<Item>();
		ListFoundItems = new ArrayList<Item>();
		DatabaseHelper myHelper = new DatabaseHelper(view);
		ListOfReportedItems = myHelper.getAllItemsForUser(account.getEmail());
		
		for (Item i : ListOfReportedItems) {
			if (i.isLostItem()) {
				ListLostItems.add(i);
			} else {
				ListFoundItems.add(i);
			}
		}
	}

	/**
	 * This will display a list of reported items by the user
	 * 
	 * @param cellLp
	 * @param rowLp
	 */
	private void addReportedItems(TableRow.LayoutParams cellLp,
			TableLayout.LayoutParams rowLp) {
		Iterator<Item> LostIterator = ListLostItems.iterator();
		Iterator<Item> FoundIterator = ListFoundItems.iterator();
		while (LostIterator.hasNext() && FoundIterator.hasNext()) {
			TableRow row = new TableRow(view);
			TextView lost = new TextView(view);
			TextView found = new TextView(view);
			Item lostItem = (Item) LostIterator.next();
			Item foundItem = (Item) FoundIterator.next();
			lost.setText(lostItem.getName());
			found.setText(foundItem.getName());
			// lost.setOnClickListener(view);
			// found.setOnClickListener(view);
			row.addView(lost, cellLp);
			row.addView(found, cellLp);
			layout.addView(row, rowLp);
		}
		Button btn = new Button(view);
		TableRow row = new TableRow(view);
		btn.setText("Back");
		btn.setOnClickListener(view);
		row.addView(btn, cellLp);
		layout.addView(row, rowLp);
	}

	/**
	 * This will show a filtered list of items that match the filter the user wants
	 * @param result the filter that the user has applied
	 */
	public void showItems(FilterResult result) {
		//Account account = DataManager.getCurrentAccount();
		String listFoundItems = "";
		String listLostItems = "";
		DatabaseHelper helper = new DatabaseHelper(view);
		List<Item> ListReportedItems = helper.getAllItems();
		ListReportedItems = manager.filterByAccount(ListReportedItems);

		switch (result) {
		case LOSTFOUND:
			ListReportedItems = manager.resetFilterItems(ListReportedItems);
			break;
		case KEEPSAKE:
			ListReportedItems = manager.filterItemsByCategory(
					ListReportedItems, "Keepsake");
			break;
		case HEIRLOOM:
			ListReportedItems = manager.filterItemsByCategory(
					ListReportedItems, "Heirloom");
			break;
		case MISC:
			ListReportedItems = manager.filterItemsByCategory(
					ListReportedItems, "Misc");
			break;
		case DATE:
			CustomDate d = view.getDateEntered();
			ListReportedItems = manager.filterItemsbyLostDate(
					ListReportedItems, d);
			break;
		case LOST:
			ListReportedItems = manager.filterLostItems(ListReportedItems);
			break;
		case FOUND:
			ListReportedItems = manager.filterFoundItems(ListReportedItems);
		}
		// for(Item i:ListReportedItems){
		// // System.out.println(i.getCategory());
		// }

		for (Item i : ListReportedItems) {
			if (i.isLostItem()) {
				listLostItems += i.getName() + '\n';
			} else if (!i.isLostItem()) {
				listFoundItems += i.getName() + '\n';
			}
		}
		view.displayItems(listLostItems, listFoundItems);
	}

	/**
	 * Takes the user back to the main menu
	 * @param intent the intent that will take them there
	 */
	public void backToMainMenu(Intent intent) {
		intent.setPackage(view.getPackageName());
		view.startActivity(intent);
	}

}